package com.zjt.smart.ui.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;

import com.zjt.smart.R;

import butterknife.Bind;

/**
 * Created by ZhongYuan on 2016/9/4.
 * 单个Fragment的Activity
 */
public abstract class SingleFragmentActivity extends ToolbarActivity {

    @Bind(R.id.app_bar)
    AppBarLayout appBar;

    @Override
    protected int layoutId() {
        return R.layout.activity_fragment;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContent();
    }

    private void initContent() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment fragment = createFragment();
        if (fragment == null) {
            return;
        }
        ft.add(R.id.frameLayout, fragment).commitAllowingStateLoss();
    }

    protected abstract Fragment createFragment();

    @Override
    protected void setToolbarParameter(Toolbar toolbar) {
        super.setToolbarParameter(toolbar);
        AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) appBar.getChildAt(0).getLayoutParams();
        if (isToolbarCanScroll()){
            params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL);
        }else {
            params.setScrollFlags(0);
        }
    }

    protected boolean isToolbarCanScroll() {
        return true;
    }

}
